/*
 * Copyright (c) Huawei Technologies Co.,Ltd. 2023. All rights reserved.
 */
package db_time;

import org.opengauss.driver.conn.ConnectionProvider;
import org.opengauss.utils.StringTool;
import common.testcase.BaseJdbcTestCase.ParseResultSet;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * Title: the DbTimeSql class.
 * <p>
 * Description:
 *
 * @author justbk
 * @version [Tools 0.0.1, 2023/8/8]
 * @since 2023/8/8
 */
@Getter
public class DbTimeSql {
    public static final String TABLE_NAME = "db_time_sql";
    public static final String CREATE_TABLE = "create table if not exists db_time_sql(id int, val1 int, val2 varchar(100), val3 timestamp);";
    public static final String DROP_TABLE = "drop table if exists db_time_sql;";
    public static final String QUERY_ALL = "select * from db_time_sql;";
    public static final String INSERT_INTO = "insert into db_time_sql (id, val1, val2, val3) values (?, ?, ?, ?)";
    
    private ConnectionProvider connectionProvider;
    public DbTimeSql(ConnectionProvider connectionProvider) {
        this.connectionProvider = connectionProvider;
    }
    
    @Getter
    @Setter
    @AllArgsConstructor
    public static class DbTimeStructVo {
        private int id;
        private int val1;
        private String val2;
        private Timestamp val3;
        
        public static DbTimeStructVo parse(ResultSet rs) throws SQLException {
            return new DbTimeStructVo(rs.getInt(1), rs.getInt(2),
                    rs.getString(3), rs.getTimestamp(4));
        }
    }
    
    public static class DbTimeStructParse implements ParseResultSet<DbTimeStructVo> {
        @Override
        public DbTimeStructVo parse(final ResultSet rs) throws SQLException {
            return DbTimeStructVo.parse(rs);
        }
    }
    public void before() throws SQLException {
        executeSql(connectionProvider.getConn(), CREATE_TABLE);
    }
    
    public void after() throws SQLException {
        executeSql(connectionProvider.getConn(), DROP_TABLE);
    }
    public void randomInsert(int begin, int end) throws SQLException {
        for (int i = begin; i < end; i ++) {
            Date curDate = new Date();
            DbTimeStructVo vo = new DbTimeStructVo(i, i*2, StringTool.randomString(10), new Timestamp(curDate.getTime()));
            insert(vo);
        }
    }
    
    public void insert(DbTimeStructVo vo) throws SQLException {
        try(PreparedStatement ps = getConnectionProvider().getConn().prepareStatement(INSERT_INTO)) {
            ps.setInt(1, vo.getId());
            ps.setInt(2, vo.getVal1());
            ps.setString(3, vo.getVal2());
            ps.setTimestamp(4, vo.getVal3());
            ps.execute();
        }
    }
    
    protected static void executeSql(Connection conn, String sql) throws SQLException {
        try (Statement st = conn.createStatement()) {
            st.execute(sql);
        }
    }
    
    public List<DbTimeStructVo> queryAll() throws SQLException{
        try (PreparedStatement ps = connectionProvider.getConn().prepareStatement(QUERY_ALL)) {
            try (ResultSet rs = ps.executeQuery()) {
                return new DbTimeStructParse().parseList(rs);
            }
        }
    }
}
